<!DOCTYPE html>
<html>
<meta charset="utf-8">

<head th:include="include :: header"></head>

<body>

<div class="ibox-content">
    <div id="jsTree"></div>
</div>
<div class="form-group ">
    <div class="col-sm-12 col-sm-offset-12">
        <button type="submit" onclick="loadUser()" class="btn btn-primary">提交</button>
    </div>
</div>

<div th:include="include :: footer"></div>
<script type="text/javascript">
    //说明：
    //parent 页面要有 loadUser(userIds, txt)方法
    //parent 页面要有 getUser()方法
    //

    $(document).ready(function () {
        getTreeData()
    });

    function getTreeData() {
        $.ajax({
            type: "GET",
            url: top.ctx + "/sys/user/tree",
            success: function (tree) {
                loadTree(tree);
            }
        });
    }

    function loadTree(tree) {
        $('#jsTree').jstree({
            'core': {
                'data': tree
            },
            "plugins": ["checkbox"]
        }).on('ready.jstree', function (event, obj) {
            $('#jsTree').jstree().open_all();
            setUser();
        });
    }

    function loadUser() {
        var userNames, userIds;
        var ref = $('#jsTree').jstree(true); // 获得整个树
        userIds = ref.get_bottom_selected();
        users = ref.get_bottom_checked('true');
        var txt = "";
        for (var user in users) {
            if (users[user].state.mType == "user") {
                if (user == 0) {
                    txt = txt + users[user].text;
                } else {
                    txt = txt + "," + users[user].text;
                }
            }
        }
        try {
            parent.loadUser(userIds, txt);
        } catch (e) {
        }
        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
        parent.layer.close(index);
    }

    function setUser() {
        try {
            $("#jsTree").jstree().check_node(parent.getUser());
        } catch (e) {
        }
    }
</script>
</body>

</html>
